import request from '@/config/axios'
import type { paramsType, resultType, paramsAdd } from './types'
import axios from 'axios'
import { useAppStore } from '@/store/modules/app'

interface IUMResponse<T = any> {
  code: number
  message: string
  result: T extends any ? T : T & any
  success: boolean
  timestamp: number
}

// 获取角色列表
export const getRoleListApi = (params: paramsType): Promise<IUMResponse<resultType>> => {
  return request.get({ url: '/sys/role/list', params })
}
// 添加角色
export const postRoleAddApi = (params: paramsAdd): Promise<IUMResponse> => {
  return request.post({ url: '/sys/role/add', data: params })
}
// 编辑角色
export const postRoleEditApi = (params: paramsAdd): Promise<IUMResponse> => {
  return request.post({ url: '/sys/role/edit', data: params })
}
// 删除角色
export const postDeleteRoleBatchApi = (ids: string): Promise<IUMResponse> => {
  return request.post({ url: `/sys/role/deleteBatch?ids=${ids}` })
}
// 根据角色Id查询用户信息
export const getUserRoleListApi = (params: any) => {
  return request.get({ url: '/sys/user/userRoleList', params })
}
// 给指定角色添加用户
export const postAddUserRole = (params: any): Promise<IUMResponse> => {
  return request.post({ url: '/sys/user/addUserRole', data: params })
}
// 删除指定角色的用户关系
export const postDeleteUserRole = (params: any): Promise<IUMResponse> => {
  return request.post({ url: '/sys/user/deleteUserRole', params })
}
// 通过角色id查询角色授权
export const getQueryRolePermission = (params: any): Promise<IUMResponse> => {
  return request.get({ url: '/sys/permission/queryRolePermission', params })
}
// 获取全部的权限树
export const getQueryTreeList = (params?: any): Promise<IUMResponse> => {
  return request.get({ url: '/sys/permission/queryTreeList', params })
}
// 保存角色授权
export const postSaveRolePermission = (data: any): Promise<IUMResponse> => {
  return request.post({ url: '/sys/permission/saveRolePermission', data })
}
// 上传头像
export const uploadAvatarApi = (file: File): Promise<any> => {
  const appStore = useAppStore()
  const formData = new FormData()
  formData.append('file', file)
  return axios({
    method: 'post',
    headers: {
      'Content-Type': 'multipart/form-data',
      'x-access-token': appStore.token
    },
    url: '/sys/user/uploadFile',
    data: formData
  })
}
